<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsFileManager extends BgcmsProcess {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
		
	public function __destruct() {
		//
	}
	
  	public function process(){
		
		if(isset($_GET['del'])){
			$file = $_GET['del'];
			if(@unlink(UPLOAD_FILES . $file))
			{
				$this->smarty->assign("message", sprintf(_("File <b>%s</b> has been deleted."), $file));
				header("Location: ./?module=BgcmsFileManager");
			}
			
		}
		if($_FILES['uploadfile']['name'] != null && $_FILES['uploadfile']['name'] != "") {
				$upload = new BgcmsUpload();
				$image = $_FILES['uploadfile']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_files($_FILES['uploadfile']['tmp_name'], "file", $image_type);
				} 
				header("Location: ./?module=BgcmsFileManager");
		}
		$this->get_files();
		
        $this->Run();
	 }
	 
	 public function get_files(){
	 	$arrImgExt = explode(",", $this->imgExt);
		$d = dir(UPLOAD_FILES);
		$file_list = array();
		if($d){
			while (false !== ($entry = $d->read())) 
			{
			   if($entry == '.' || $entry == '..' || $entry == 'thumb')	
					continue;
					
				if(is_dir($d->path . $entry))
				{
				   $arrTemp = $this->dir_walker($d->path . $entry);
				   $file_list = array_merge_recursive($file_list, $arrTemp);
				}
				else
				{
					$idx = count($file_list);
				   $file_list[$idx]['name'] = $entry;
				   $file_list[$idx]['img'] = "../".PATH_FILES.$entry;
				   $file_list[$idx]['link'] =	PATH_FILES.$entry;
				   $file_list[$idx]['type'] = '';	//not for download
				   
				   $pathInfo = pathinfo($entry);
				   
				   $file_list[$idx]['ext'] = $pathInfo['extension'];
				   
				  if(in_array(strtolower($file_list[$idx]['ext']), $arrImgExt))
				   {
				   		$file_list[$idx]['image'] = "1";
				   }
				}
			}

			$d->close();
		}
		if(isset($_POST['changeShowImg'])){
			$_SESSION['showImg'] = 0;
			if(isset($_POST['showImg']))
			{
				$_SESSION['showImg'] = "1";
			}
		}
		
		$cut = 0;
		for($j=0;$j<count($file_list);$j++){
			if($file_list[$j]['image']!='1'){
				$allfile[$cut] = $file_list[$j];
				$cut++;
			}	
		}
		$total = count($allfile);
		if($total>0){
			if($total>ADMINPAGING)
				$fileinpage = ADMINPAGING;
			else	
				$fileinpage = $total;
		$totalpage= ceil($total/$fileinpage);
		if(isset($_GET['list']) && $_GET['list']!="")
			$page = $_GET['list'];
		else
			$page = '1';
		if($page == '1'){
			$start = 0;
			$end =$fileinpage;
		}else{
			$start= ($page*$fileinpage)-$fileinpage;
			$end =$start + $fileinpage;
			if($end>=$total)
				$end = $total;
		}
		$dem = 0;	
		for($i=$start;$i<$end;$i++){
			$file[$dem] = $allfile[$i];
			$dem++;
			
		}
		$arrfile['data'] = $file;
		$result = BgcmsPaging::do_paging($total,$arrfile, ADMINPAGING,"?module=BgcmsFileManager","list");
		$this->smarty->assign("FILES", $result);
		}
	 }
	 function dir_walker($sdir)
	{
		$arrImgExt = explode(",", $this->imgExt);
		
		$d = dir($sdir);
		$file_list = array();
		if($d)
		{
			while (false !== ($entry = $d->read())) 
			{
			   if($entry == '.' || $entry == '..' || $entry == 'thumb')	
					continue;
					
				if(is_dir($d->path . $entry))
				{
				   $arrTemp = $this->dir_walker($d->path . $entry);
				   $file_list = array_merge_recursive($file_list , $arrTemp);
				}
				else
				{
					$idx = count($file_list);
				   $file_list[$idx]['name'] = $entry;
				    $file_list[$idx]['img'] = "../".PATH_FILES.$entry;
				   $file_list[$idx]['link'] = $entry;
				   $file_list[$idx]['type'] = basename($sdir);
				   
				   $pathInfo = pathinfo($entry);
				   
				   $file_list[$idx]['ext'] = $pathInfo['extension'];
				   
				   if(in_array(strtolower($file_list[$idx]['ext']), $arrImgExt))
				   {
				   		$file_list[$idx]['image'] = "1";
				   }
				}
			}

			$d->close();
		}
		
		return $file_list;
	}
}
?>